<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use DB;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class ShiwuController extends Controller
{
	// 识物
	public function index()
	{	
		// 投稿入口
		$tougao = DB::table('gposts')->where('gpoint','投稿')->where('gpstatus','已通过')->orderBy('gpctime','desc')->first();
		
		// 	本周鞋报
		$xiebao = DB::table('gposts')->where('gpoint','本周鞋报')->where('gpstatus','已通过')->orderBy('gpctime','desc')->first();
		
		// 一周鞋子精选
		$xzjingxuan = DB::table('gposts')->where('gpoint','一周精选')->where('gpstatus','已通过')->orderBy('gpctime','desc')->first();

		// 浏览量最高
		$liulanm = DB::table('gposts')->where('gpstatus','已通过')->orderBy('gpview','desc')->first();

		// 最热
		$zuire = DB::table('gposts')->where('gpstatus','已通过')->orderBy('gplike','desc')->first();

		// 热门文章
		$remen = DB::table('gposts')->where('gpstatus','已通过')->orderBy('gplike','desc')->skip(1)->take(4)->get();
		// dd($remen);
		// 热门测评
		$ceping1 = DB::table('gposts')->where('gpoint','测评')->where('gpstatus','已通过')->orderBy('gpctime','desc')->first();
		$ceping2 = DB::table('gposts')->where('gpoint','测评')->where('gpstatus','已通过')->orderBy('gpctime','desc')->skip(1)->take(3)->get();




		//解析模版
		return view('home.shihuo.shiwu.index',['tougao'=>$tougao,'xiebao'=>$xiebao,'xzjingxuan'=>$xzjingxuan,'zuire'=>$zuire,'remen'=>$remen,'liulanm'=>$liulanm]);
	}

	// 识货文章详情
	public function getDetails(Request $request)
	{	
		// 浏览量+1
		DB::update('update gposts set gpview = gpview+1 where gpid ='.$request->gpid);
		// 文章详情
		$details = DB::table('gposts')->select(DB::raw('gposts.*,users.username,users.pic'))->leftjoin('users','gposts.gpuid','=','users.uid')->where('gpid',$request->gpid)->first();
		// dd($details);
		// 评论详情
		$gpcomments = DB::select('select gpcomments.*,users.username,users.pic from gpcomments left join users on gpcomments.gpcuid = users.uid where gpcomments.gpid = '.$request->gpid.' order by gpcomments.gpctime desc');
		// dd($gpcomments);
		// 评论条数
		$gpcnum = DB::select('select count(*) as gpcnum from gpcomments where gpid ='.$request->gpid)[0];

		// 解析模板
		return view('home.shihuo.shiwu.details',['details'=>$details,'gpcomments'=>$gpcomments,'gpcnum'=>$gpcnum]);
	}

	// 查看文章评论等 ajax
	public function getComment()
	{
		// 将帖子id 评论者 评论内容 存入数组
		$data['gpid'] = $_GET['gpid'];
		// $data['gpcuid'] = session('uid');
		$data['gpcont'] = $_GET['gpcont'];
		$data['gpctime'] = date('Y-m-d H:i:s',time()); // 写入事件

		// 写入数据库
		$gpcid = DB::table('gpcomments')->insertGetId($data);

		// 查询刚添加的信息
		$comment = DB::select('select gpcomments.*,users.username,users.pic from gpcomments left join users on gpcomments.gpcuid = users.uid where gpcomments.gpcid ='.$gpcid);
		
		echo json_encode($comment);
	}

	// 添加收藏
	public function getShoucang()
	{
		$data['gpid'] = $_GET['gpid'];
		// $data['uid'] = session('uid');
		$data['gpstime'] = date('Y-m-d H:i:s',time());

		// 添加
		$res = DB::table('shihuoshoucang')->insert($data);
		if($res){
			echo  1;
		}
	}

	// 判断用户是否收藏
	public function getSfsc()
	{
		$data['gpid'] = $_GET['gpid'];
		$res = table('shihuoshoucang')->where('uid',session('uid'))->where('gpid',$gpid)->get();
		if($res){
			echo 1;
		}
	}

	// 判断是否登录
	public function getSfdl()
	{
		if(session('uid')){
			echo 1;
		}
	}

	// 文章点赞
	public function getWenzhangzan()
	{
		$gpid = $_GET['gpid'];

		$res = DB::update('update gposts set gplike = gplike+1 where gpid ='.$gpid);

		if($res){
			$gplike = DB::select('select gplike from gposts where gpid = '.$gpid);
			echo json_encode($gplike);
		}
	}

	// 评论点赞
	public function getPinglunzan()
	{	
		$gpcid = $_GET['gpcid'];

		$res = DB::update('update gpcomments set gpclike = gpclike+1 where gpcid ='.$gpcid);

		if($res){
			$gpclike = DB::select('select gpclike from gpcomments where gpcid = '.$gpcid);
			echo json_encode($gpclike);
		}
	}

	// 投稿
	public function getTougao()
	{
		return view('home.shihuo.shiwu.tougao');
	}

	// 处理投稿
	public function postAddtougao(Request $request)
	{
		dd($request->all());
		$data = $request->only('gptitle','gpoint');
		$data['gpcont'] = $request->editorValue;
		$data['gpic'] = $this->upload($request,'gpic');  //调用函数处理图片;
		// $data['uid'] = session('uid');
		$data['gpctime'] = date('Y-m-d H:i:s',time());

		$res = DB::table('gposts')->insert($data);

		if($res){
	        return redirect('/home/shiwu/index')->with('success','发帖成功');
	    }else{
	        return back()->with('error','发帖失败');
	    }
	}

	 // 封装一个方法用来处理分类图片上传
    private function upload($request,$file)
    {
        if($request->hasFile($file)){
            $suffix = $request->file($file)->getClientOriginalExtension(); // 获取文件后缀名
            $name = md5(time()).rand(0,99999);  //生成一个随机文件名
            $request->file($file)->move('./uploads/gposts/',$name.'.'.$suffix);  //移动
            return '/uploads/gposts/'.$name.'.'.$suffix;  //返回路径信息
        }
    }

}
